//
//  magazineViewController.m
//  ILiangCang
//
//  Created by ZX on 16-4-11.
//  Copyright (c) 2016年 CRQ. All rights reserved.
//

#import "magazineViewController.h"
#import "LGSegment.h"
#import "magezineView.h"
#import "topLeft.h"
#import "topRight.h"
#import "MJRefresh.h"
#import "Masonry.h"
#import "infosModel.h"
#import "publicWebViewController.h"

@interface magazineViewController ()<UIScrollViewDelegate,SegmentDelegate,magezineViewDelegate>
{
    UIView * topView;
    UIView * navView;
    UIView * magaView;
    
    
}
#define LG_ScreenW [UIScreen mainScreen].bounds.size.width
#define LG_ScreenH [UIScreen mainScreen].bounds.size.height
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGTH [UIScreen mainScreen].bounds.size.height
//ScrollView高度
#define LG_scrollViewH [UIScreen mainScreen].bounds.size.height-64
//Segment高度
#define LG_segmentH 64

@property (nonatomic, strong) UIScrollView *contentScrollView;
@property(nonatomic,strong)NSMutableArray *buttonList;
@property (nonatomic, weak) LGSegment *segment;
@property(nonatomic,weak)CALayer *LGLayer;
@end

@implementation magazineViewController

- (NSMutableArray *)buttonList
{
    if (!_buttonList)
    {
        _buttonList = [NSMutableArray array];
    }
    return _buttonList;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = YES;
               [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    self.view.autoresizesSubviews = YES;
    topView = [[UIView alloc]init];
    topView.backgroundColor = [UIColor whiteColor];
    topView.frame = CGRectMake(0, -(HEIGTH-64), WIDTH, HEIGTH-64);
    [self.view addSubview:topView];
    
    //加载Segment
    [self setSegment];
    //加载ViewController
    [self addChildViewController];
    //加载ScrollView
    [self setContentScrollView];

    navView = [[UIView alloc]init];
    navView.backgroundColor = [UIColor blackColor];
    navView.frame = CGRectMake(0, 0, WIDTH, 64);
    [self.view addSubview:navView];
    UIButton * navBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    navBtn.frame = CGRectMake(0, 20, WIDTH, 44);
    [navBtn setBackgroundColor:[UIColor clearColor]];
    [navBtn setTitle:[NSString stringWithFormat:@"杂志"] forState:UIControlStateNormal];
    [navBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [navBtn addTarget:self action:@selector(navBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    navBtn.tag = 1;
    [navView addSubview:navBtn];
    
    
     _dataLab = [[UILabel alloc]init];
    _dataLab.text = @"TODAY";
    _dataLab.font = [UIFont systemFontOfSize:14];
    _dataLab.textColor = [UIColor whiteColor];
    [navView addSubview:_dataLab];
    [_dataLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(navView.mas_left).with.offset(15);
        make.bottom.mas_equalTo(navView.mas_bottom).with.offset(-13);
        
        
    }];
    
    
    //视图 magaview
    magaView = [[UIView alloc]init];
    magaView.backgroundColor = [UIColor darkGrayColor];
    magaView.frame = CGRectMake(0, 64, WIDTH, HEIGTH-64);
    [self.view addSubview:magaView];
    
    //视图类 magazineView
    magezineView * magazineView = [[magezineView alloc]initWithFrame:self.view.frame];
    magazineView.delegate = self;
    [magaView addSubview:magazineView];
    
    
    
}
-(void)viewWillAppear:(BOOL)animated
{
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}

-(void)setSegment {
    
    [self buttonList];
    //初始化
    LGSegment *segment = [[LGSegment alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, LG_segmentH)];
    segment.delegate = self;
    self.segment = segment;
    [topView addSubview:segment];
    [self.buttonList addObject:segment.buttonList];
    self.LGLayer = segment.LGLayer;
    
}
//加载ScrollView
-(void)setContentScrollView {
    
    UIScrollView *sv = [[UIScrollView alloc]initWithFrame:CGRectMake(0, LG_segmentH, self.view.frame.size.width, LG_scrollViewH)];
    [topView addSubview:sv];
    sv.bounces = NO;
    sv.contentInset = UIEdgeInsetsMake(-20, 0, 0, 0);
    sv.contentOffset = CGPointMake(0, 0);
    sv.pagingEnabled = YES;
    sv.showsHorizontalScrollIndicator = NO;
    sv.scrollEnabled = YES;
    sv.userInteractionEnabled = YES;
    sv.delegate = self;
    
    for (int i=0; i<self.childViewControllers.count; i++) {
        UIViewController * vc = self.childViewControllers[i];
        vc.view.frame = CGRectMake(i * LG_ScreenW, 0, LG_ScreenW, LG_scrollViewH);
        [sv addSubview:vc.view];
        
    }
    
    sv.contentSize = CGSizeMake(2 * LG_ScreenW, 0);
    self.contentScrollView = sv;
}
//加载2个ViewController
-(void)addChildViewController{
    
    topLeft * vc1 = [[topLeft alloc]init];

    [self addChildViewController:vc1];
    topRight * vc2 = [[topRight alloc]init];

    [self addChildViewController:vc2];

}

#pragma mark - UIScrollViewDelegate
//实现LGSegment代理方法
-(void)scrollToPage:(int)Page {
    CGPoint offset = self.contentScrollView.contentOffset;
    offset.x = self.view.frame.size.width * Page;
    [UIView animateWithDuration:0.3 animations:^{
        self.contentScrollView.contentOffset = offset;
    }];
}
// 只要滚动UIScrollView就会调用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    CGFloat offsetX = scrollView.contentOffset.x;
    [self.segment moveToOffsetX:offsetX];
    
}

-(void)navBtnClick:(UIButton *)btn
{
    
    btn.tag++;
    if (btn.tag % 2 == 0)
    {
        [UIView animateWithDuration:0.6 animations:^{
            
            topView.frame = CGRectMake(0, 0, WIDTH, HEIGTH-64);
            navView.frame = CGRectMake(0, HEIGTH-64, WIDTH, 64);
            magaView.frame = CGRectMake(0,HEIGTH-1, WIDTH, HEIGTH-63);
            [self hideTabBar:YES];
            
        }];
   }
    
    else{

        [UIView animateWithDuration:0.6 animations:^{
            topView.frame = CGRectMake(0, -(HEIGTH-64), WIDTH, HEIGTH-64);
            navView.frame = CGRectMake(0, 0, WIDTH, 64);
            magaView.frame = CGRectMake(0,64, WIDTH, HEIGTH-64);
            [self hideTabBar:NO];
        }];
        
    }
}

- (void) hideTabBar:(BOOL) hidden{
    
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.6];
    
    for(UIView *view in self.tabBarController.view.subviews)
    {
        if([view isKindOfClass:[UITabBar class]])
        {
            if (hidden) {
                [view setFrame:CGRectMake(0, HEIGTH, WIDTH, view.frame.size.height)];
                view.alpha = 0;
                
            } else {
                [view setFrame:CGRectMake(0, HEIGTH-49, WIDTH, view.frame.size.height)];
                view.alpha = 1;
            }
        }
        else
        {
            if (hidden) {
                [view setFrame:CGRectMake(0, view.frame.origin.y, view.frame.size.width, view.frame.size.height)];
                
            } else {
                [view setFrame:CGRectMake(0, view.frame.origin.y, WIDTH,  HEIGTH-49)];
                
            }
        }
    }
    
    [UIView commitAnimations];
}

#pragma --------magazineView 代理方法
-(void)magazineCellDidSelectedSection:(NSInteger)section Row:(NSInteger)row with:(NSArray *)sectionArray and:(NSArray *)rowArray
{
    NSArray *array=rowArray[section];//找到小数组
    infosModel * info = array[row];//小数组内部的对象
    
    publicWebViewController * vc = [[publicWebViewController alloc]init];
    vc.url = info.topicURL;
    vc.title = info.topicName;
    [self.navigationController pushViewController:vc animated:YES];

}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
